vLastAutoTargetCheck = nil

function AutoTarget()
	if not ccRS.Auto_Target then return end
	-- Clear target
	if UnitIsDead("Target")
	or (UnitIsFriend("Player", "Target") == 1 and UnitCanAttack("Player","Target") == nil)
	or UnitCreatureType("Target") == "Critter"
	or UnitCreatureType("Target") == "Wild Pet"
	then
		ClearTarget() 
	end;
	-- We already have a target, Exit.
	if UnitExists("Target") == 1 then return end
	--
	objPlayerGetNearbyEnemies(true)
	-- Find the closest target and target
	local vObj = nil
	local vDistance = 40
	local tbl = tblPGetNearbyEnemies
	for i = 1, # tbl do
		if tbl[i]
		and tbl[i]:Exists()
		and tbl[i]:InLineOfSight()
		and tContains(tblAutoTargetIgnoreList, tbl[i]:GetName()) == nil
		-- Custom check's to see if it's a specific npc with a immune buff
			-- Sha of Doubt - Bounds of Reality - Immune
			and tbl[i]:GetAura(GetSpellInfo(117665)) == nil
			-- Gu Cloudstrike - Charging Soul - Immune
			and tbl[i]:GetAura(GetSpellInfo(110945)) == nil
			-- Siamat - XXXX - Immune
		and (
			Player:IsFacing(tbl[i]) 
			or
				(
				tbl[i]:GetDistance(Player) <= 12
				and
				tbl[i]:IsInCombat()
				)
			)
		then
			if tbl[i]:GetDistance(Player) < vDistance then
				vObj = tbl[i]
				vDistance = tbl[i]:GetDistance(Player)
			end
		end
	end

	if vObj ~= nil then vObj:Target() end

end
-- Immune Aura, If found clear target
tblAutoTargetImmuneList = {
	117665,	-- Sha of Doubt - Bounds of Reality - Immune
	110945,	-- Gu Cloudstrike - Charging Soul - Immune

}
-- Ignore useless shit we do not want to accidently target
tblAutoTargetIgnoreList = {
	"Defias Cannon",
	"Mining Powder",
	"Manifestation",
}

function AutoTarget_old()

	if not ccRS.Auto_Target then return end

	-- Clear target
	if UnitIsDead("Target") == 1 then ClearTarget() end;
	if UnitIsFriend("Player", "Target") == 1
	and UnitCanAttack("Player","Target") == nil
	then
		ClearTarget() 
	end;
	if UnitCreatureType("Target") == "Critter" then ClearTarget() end;
	if UnitCreatureType("Target") == "Wild Pet" then ClearTarget() end;

	-- If we have a target
	if Target
	and Target:Exists()
	and not Target:IsDead() then
		-- Clear target if we cannot attack it
		if not Player:CanAttack(Target) then ClearTarget() end;
	end

	-- We have a target, Exit.
	if UnitExists("Target") == 1 then return end

	-- If we dont have a target
	if vLastAutoTargetCheck == nil
	or (GetTime()-vLastAutoTargetCheck > ccRS.AutoTargetDelay)
	then
		-- Clear old data if exists
		ccrHostileMobTable = {};
			--[[
				Ignore list

				Wise Mari - Water bubble
				Sha of Doubt - Bounds of Reality
				
			]]
		-- Get all Hostile mobs from ObjTable
		for i = 0, # ccrObjectListTable do
			local TableObject = ccrObjectListTable[i]
			if TableObject ~= nil
			and TableObject:Exists()
			and (TableObject:GetType() == 3 or TableObject:GetType() == 4)
			and TableObject:GetDistance(Player) <= 44
			and TableObject:GetCreatureInfo() ~= "Critter"
			and TableObject:GetCreatureInfo() ~= "Wild Pet"
			and TableObject:InLineOfSight()
			and not TableObject:IsDead()
			and Player:CanAttack(TableObject)
			and Player:IsFacing(TableObject)		
			--and WorldToScreen(TableObject:GetLocation()) ~= nil
			then
				--local tblX, tblY = TableObject:GetLocation()
				--if IsPointInCone(tblX, tblY, 170) then
					table.insert(ccrHostileMobTable, i .. "," .. TableObject:GetDistance())
				--end
			end
		end
		-- Find the closest target
		local vRange = 44
		local tblPosition = nil
		for i = 0, # ccrHostileMobTable do
			if ccrHostileMobTable[i] ~= nil then
				objPosition, objDistance = strsplit(",", ccrHostileMobTable[i])
				objDistance = tonumber(objDistance)
				if objDistance < vRange then
					vRange = objDistance
					tblPosition = tonumber(objPosition)
				end
			end
		end

		objToTarget = nil
		if tblPosition ~= nil then objToTarget = ccrObjectListTable[tblPosition] end;
		if objToTarget ~= nil
		and objToTarget:Exists()
		and (objToTarget:GetType() == 3 or objToTarget:GetType() == 4)
		and objToTarget:GetCreatureInfo() ~= "Critter"
		and objToTarget:GetCreatureInfo() ~= "Wild Pet"
		and objToTarget:InLineOfSight()
		and not objToTarget:IsDead()
		and Player:CanAttack(objToTarget)
		and Player:IsFacing(objToTarget)
		then
			objToTarget:Target()
		end
		vLastAutoTargetCheck = GetTime()
	end
end

function IsPointInCone (X, Y, Radians)
	Radians = rad(Radians)
	local PlayerX, PlayerY, PlayerZ, PlayerR = Player:GetLocation();
	local FirstX, FirstY = PlayerX + 10000 * math.cos(PlayerR - (Radians / 2)), PlayerY + 10000 * math.sin(PlayerR - (Radians / 2));
	local SecondX, SecondY = PlayerX + 10000 * math.cos(PlayerR + (Radians / 2)), PlayerY + 10000 * math.sin(PlayerR + (Radians / 2));
	if X >= FirstX
	and X <= SecondX
	and Y >= FirstY
	and Y <= SecondY
	then
		return true
	else
		return false
	end
end